package com.firemoss.atonement.mapper.resolution
{
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.metadata.Property;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import org.as3commons.reflect.Type;

	public class CollectionValueResolverHelper
	{
		public static const resolveableSimpleTypes : Dictionary = _resolveableSimpleTypes()
		
		private static function _resolveableSimpleTypes() : Dictionary
		{
			var dict : Dictionary = new Dictionary()
			dict[ Type.forClass( String ) ] = true
			dict[ Type.forClass( Number ) ] = true
			dict[ Type.forClass( Date ) ] = true
			dict[ Type.forClass( Boolean ) ] = true
			dict[ Type.forClass( ByteArray ) ]  =  true
			return dict
		}
			
		
			
		public static function createCollectionItem( source : *, sourceItem : *, targetProperty : Property, mapping : Mapping, mappingChain : Dictionary, targetItemClass : Class = null ) : *
		{
			if ( !targetItemClass )
			{
				targetItemClass = mapping.aggregatedPropertyTargetTypes[ targetProperty.name ]
			}
			
			var sourceItemType : Type = Type.forInstance( sourceItem )
			var sourceItemClass : Class = sourceItemType.clazz
				
			// Collection of simples:  return simple
			if ( resolveableSimpleTypes[ sourceItemType ] )
			{
				return sourceItem
			}
			// Collection of complex:  perform mapping
			else
			{
				var targetItem : * = mapping.mapper.graphedInstanceFactory.createByTargetClass( sourceItem, targetItemClass )
				return mapping.mapper.map( sourceItem, targetItem, mappingChain )
			}
		}
		
	}
}